package com.rocky.server.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.apache.log4j.Logger;

import com.rocky.server.Global;
import com.rocky.server.database.DBAdapter;
import com.rocky.server.model.User;
import com.rocky.server.util.Util;

public class UserService {
	private static Logger logger = Logger.getLogger(UserService.class);
	
	/**
	 * 登陆验证
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean isValidate(String username,String password){
		if(Global.userList.get(username) != null && password.equals(Global.userList.get(username).getPassword())){
			return true;
		}
		return false;
	}
	
	public boolean addFirend(int userid,int firendid){
		DBAdapter db = new DBAdapter();
		try{
			PreparedStatement pStmt = db.con.prepareStatement("insert into friend (`userid`,`friendid`) values (?,?)");
			pStmt.setInt(1, userid);
			pStmt.setInt(2, firendid);
			int flag = pStmt.executeUpdate();
			if(flag > 0){
				return true;
			}else{
				return false;
			}
		}catch (SQLException e) {
			logger.error(e.toString());
		}finally{
			db.closeConnect();
		}
		return false;
	}
	
	public boolean uploadPhoto(String username,String photo){
		DBAdapter db = new DBAdapter();
		try {
			String sql = "update user set `photo` = '" + photo + "' where username = '"+username+"'";
			logger.debug("update photo to database : " + sql);
			int flag = db.stmt.executeUpdate(sql);
			if(flag > 0){
				logger.debug("success.");
				return true;
			}
		} catch (SQLException e) {
			logger.error(e.toString());
		}finally{
			db.closeConnect();
		}
		logger.debug("fail.");
		return false;
	}

	public boolean registerUser(String username,String password,String nickname,String birthday,int sex){
		DBAdapter db = new DBAdapter();
		Timestamp tt=new Timestamp(Util.getDateByString(birthday).getTime());
		try {
			PreparedStatement pStmt = db.con.prepareStatement("insert into user (`username`,`password`,`nickname`,`birthday`,`sex`) values (?,?,?,?,?)");
			pStmt.setString(1, username);
			pStmt.setString(2, password);
			pStmt.setString(3, nickname);
			pStmt.setTimestamp(4, tt);
			pStmt.setInt(5, sex);
			int flag = pStmt.executeUpdate();
			if(flag>0){
				ResultSet rs = db.stmt.executeQuery("select LAST_INSERT_ID()");
				while(rs.next()){
					int userid = rs.getInt(1);
					User user = new User(userid,username,password,nickname,Util.getDateByString(birthday),sex);
					Global.userList.put(user.getUsername(), user);
					break;
				}
				return true;
			}
		} catch (SQLException e) {
			logger.error(e.toString());
		}finally{
			db.closeConnect();
		}
		return false;
	}
	
	public boolean addPoint(int userid,String location_key,String location){
		DBAdapter db = new DBAdapter();
		try {
			String selectSql = "select * from point where userid = " + userid;
			db.rs = db.stmt.executeQuery(selectSql);
			if(db.rs.next()){
				String updateSql = "update point set `location_key` = '" + location_key + "',`location` = '" + location + "' where userid = " + userid;
				int flag = db.stmt.executeUpdate(updateSql);
				if(flag > 0){
					return true;
				}
			}else{
				PreparedStatement pStmt = db.con.prepareStatement("insert into point (`userid`,`location_key`,`location`) values (?,?,?)");
				pStmt.setInt(1, userid);
				pStmt.setString(2, location_key);
				pStmt.setString(3, location);
				int flag = pStmt.executeUpdate();
				if(flag>0){
					ResultSet rs = db.stmt.executeQuery("select LAST_INSERT_ID()");
					while(rs.next()){
						int pointid = rs.getInt(1);
						break;
					}
					return true;
				}
			}
		} catch (SQLException e) {
			logger.error(e.toString());
		}finally{
			db.closeConnect();
		}
		return false;
	}
	
	public boolean updateMood(int userid,String mood){
		DBAdapter db = new DBAdapter();
		try {
			String sql = "update user set `mood` = '" + mood + "' where userid = "+userid;
			logger.debug("update mood to database : " + sql);
			int flag = db.stmt.executeUpdate(sql);
			if(flag > 0){
				logger.debug("success.");
				return true;
			}
		} catch (SQLException e) {
			logger.error(e.toString());
		}finally{
			db.closeConnect();
		}
		logger.debug("fail.");
		return false;
	}
	
	public boolean updateLocation(int userid,double location_x,double location_y,String location_key){
		DBAdapter db = new DBAdapter();
		try {
			String sql = "update user set `location_x` = " + location_x+",`location_y` = "+location_y+",`location_key` = '" + location_key + "' where userid = "+userid;
			logger.debug("update location to database : " + sql);
			int flag = db.stmt.executeUpdate(sql);
			if(flag > 0){
				logger.debug("success.");
				return true;
			}
		} catch (SQLException e) {
			logger.error(e.toString());
		}finally{
			db.closeConnect();
		}
		logger.debug("fail.");
		return false;
	}
}
